package com.atguigu.flume.interceptor;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class LogETLInterceptor implements Interceptor {


    @Override
    public void initialize() {

    }

    @Override
    public Event intercept(Event event) {

        // 实现我们的逻辑
        //获取数据 =》 string [set utf-8] =>调用我们的工具类，对event start 两种日志进行校验
//        byte[] body = event.getBody();
//        String log = new String( body , Charset.forName("UTF-8"));
//        if(log.contains("start")){
//            if(LogUtils.validateStart(log)){
//                return event ;
//            }
//        }
//        else {
//            if(LogUtils.validateEvent(log)){
//                return  event ;
//            }
//
//        }
//
//        return null;
        return  event ;
    }

    /**
     * 对批量的event 调用已经写好的东西进行循环输出
     * @param events
     * @return
     */
    @Override
    public List<Event> intercept(List<Event> events) {

        //初始化值
        ArrayList<Event> list = new ArrayList<Event>() ;
        for (Event event :events ) {
            Event intercept = intercept(event);
            list.add( intercept) ;
        }
        return list;
    }

    @Override
    public void close() {

    }
    // 单列对象的build 的实现
    public static class  Builder  implements   Interceptor.Builder {

        @Override
        public Interceptor build() {
            return  new LogETLInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }
}
